#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
此脚本主要用于维持系统的网络连接（桂电）
	--主要服务于长期不关机作为服务器的系统

适用平台：Windows/Linux
所需环境：python3
所需包：  requests
使用方法：
	Windows
		win+r 打开运行 输入cmd 回车
		调整路径至本文件所在路径
		执行python3 Internet.py

	Linux
		打开一个终端 调整路径至本文件所在路径
		执行python3 Internet.py

注：如果没有安装requests库，自行百度安装
'''
import requests
from random import choice
import time
import os
import platform

# 查询联网周期,单位s，可根据自己需求更改
peried = 30

# 可用账户，可自己添加或者默认使用下列账户
form_list = (
    ('1600570106', '310020'),
    ('1600570107', '166449'),
    ('1600570108', '034420'),
    ('1600570109', '224261'),
    ('1600570110', '031826'),
    ('1600570111', '191020'),
    ('1600570112', '153628'),
    ('1600570113', '270929'),
    ('1600570114', '291523'),
    ('1600570115', '270626'),
    ('1600570116', '160021'),
    ('1600570117', '30352X'),
    ('1600570118', '060042'),
    ('1600570119', '18466X'),
    ('1600570120', '126520'),
    ('1600570121', '196029'),
    ('1600570122', '088387'),
    ('1600570123', '02564X'),
    ('1600570124', '220420'),
    ('1600570125', '12359A'),
    ('1600570126', '286912'),
    ('1600570127', '071014'),
    ('1600570208', '014726'),
    ('1600570210', '205382'),
    ('1600570212', '080645'),
)

url_post = 'http://10.36.254.11/a70.htm'
url_get = 'http://10.36.254.11/F.htm'
data = {
    '0MKKey': '123456',
    'buttonClicked': '',
    'cmd': '',
    'DDDDD': '',
    'err_flag': '',
    'Login': '',
    'para': '00',
    'password': '',
    'R1': '0',
    'R2': '',
    'R3': '0',
    'R6': '0',
    'redirect_url': '',
    'upass': '',
    'user': '',
    'username': ''
}
headers = {
    'Cookie': 'program=dongqu_2018041101; vlan=0; ip=202.193.56.33; md5_login2=1600570129%7C072739',
    'Host': '10.36.254.11',
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}


def Open_Net():
    '''Open the Internet'''
    requests.post(url_post, data=data)
    print("Connect OK!")


'''
    def Close_Net:
        ''''''
        requests.get(url_get, headers=headers)
'''


def Check_Net():
    if 'Windows' in platform.platform():
        code = os.system('ping www.baidu.com')
    else:
        code = os.system('ping -c4 www.baidu.com')
    if code:
        print("Connect statue: False")
        return False
    else:
        print("Connect statue: True")
        return True


def Change_data():
    l = choice(form_list)
    data['DDDDD'] = l[0]
    data['upass'] = l[1]
    print("帐号：", l[0])
    print("密码：", l[1])


def main():
    while True:
        try:
            if Check_Net() == False:
                Change_data()
                Open_Net()
        except Exception as e:
            print(e)
        time.sleep(peried)


if __name__ == '__main__':
    main()